﻿//    Copyright 2014 LuoZhihui
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//      http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#region  导入名称空间

#endregion

#region  导入名称空间

using System;
using System.Collections.Generic;
using LotteryEngine.Infrastructure.Querying;
using LotteryEngine.Infrastructure.UnitOfWork;
using LotteryEngine.Model;
using LotteryEngine.Model.Repositories;
using LotteryEngine.Services.Interfaces;

#endregion

namespace LotteryEngine.Services.Impl
{
	/// <summary>
	///     抽奖活动领域服务
	/// </summary>
	public class RecordService : IRecordService
	{
		private readonly IRecordRepository _recordRepository;
		private readonly IUnitOfWork _unitOfWork;

		public RecordService(IRecordRepository recordRepository, IUnitOfWork unitOfWork)
		{
			_recordRepository = recordRepository;
			_unitOfWork = unitOfWork;
		}


		public void Save(Record record)
		{
			_recordRepository.Add(record);
			_unitOfWork.Commit();
		}

		/// <summary>
		///     活动参与玩家
		/// </summary>
		public IEnumerable<Record> GetPlayers(Sweepstake sweepstake, int index, int count)
		{
			var query = new Query();
			query.Add(Criterion.Create<Record>(_record => _record.Sweepstake.Id, sweepstake.Id, CriteriaOperator.Eq));
			query.QueryOperator = QueryOperator.And;
			var result = _recordRepository.FindBy(query, index, count);

			return result;
		}

		/// <summary>
		///     活动玩家中奖纪录
		/// </summary>
		public IEnumerable<Record> GetAllWinPlayers(Sweepstake sweepstake, int index, int count)
		{
			var query = new Query();
			query.Add(Criterion.Create<Record>(_record => _record.Sweepstake.Id, sweepstake.Id, CriteriaOperator.Eq));
			query.Add(Criterion.Create<Record>(_record => _record.PlayResult, true, CriteriaOperator.Eq));
			query.QueryOperator = QueryOperator.And;
			var result = _recordRepository.FindBy(query, index, count);


			return result;
		}

		/// <summary>
		///     活动今日中奖玩家纪录
		/// </summary>
		public IEnumerable<Record> GetTodayWinPlayers(Sweepstake sweepstake)
		{
			var query = new Query();
			query.Add(Criterion.Create<Record>(_record => _record.Sweepstake.Id, sweepstake.Id, CriteriaOperator.Eq));
			query.Add(Criterion.Create<Record>(_record => _record.PlayResult, true, CriteriaOperator.Eq));
			query.Add(Criterion.Create<Record>(_record => _record.PlayTime, DateTime.Today, CriteriaOperator.Eq));
			query.QueryOperator = QueryOperator.And;
			var result = _recordRepository.FindBy(query);


			return result;
		}
	}
}